let path = require('path');

let HtmlWebpackPlugin = require('html-webpack-plugin');
let CopyWebpackPlugin = require('copy-webpack-plugin');
let webpack = require('webpack');

module.exports = {
  mode : 'production', // development | production
  entry : {
    'aurora/gallery' : './src/show/gallery.js',
    'aurora/monitor' : './src/monitor/index.js',
    'aurora/toolbox' : './src/toolbox/index.js',
  },
  output : {
    filename : '[name].js',
    chunkFilename: '_bundle/[name].bundle.js',
    path : path.resolve(__dirname, 'dist'),
    publicPath: "/"
  },

  devtool : 'cheap-module-source-map',

  // server
  devServer : {
    port : 3000,
    contentBase : 'dist',
    before : function (app) {
      app.get('/test', (request, response) => {
        response.json({result : true, message : 'test 成功'});
      });
      app.get('/aurora/gallery', (request, response) => {
        response.redirect('/aurora/gallery.html');
      });
      app.get('/aurora/monitor', (request, response) => {
        response.redirect('/aurora/monitor.html');
      });
      app.get('/', (request, response) => {
        response.redirect('/aurora/gallery');
      });
      app.get('/aurora/asset/allSeries', (request, response) => {
        setTimeout(() => {response.redirect('/aurora/asset/allSeries.json');}, 500);
      });
      app.get('/aurora/monitor/allSeries', (request, response) => {
        setTimeout(() => {response.redirect('/aurora/monitor/monitor-allseries.json');}, 500);
      });
      app.get('/aurora/monitor/status/all', (request, response) => {
        response.redirect('/aurora/asset/statusAll.json');
      });
    }
  },

  plugins : [
    new HtmlWebpackPlugin({
      template : './public/gallery.html',
      filename : 'aurora/gallery.html',
      chunks : ['aurora/gallery']
    }),
    new HtmlWebpackPlugin({
      template : './public/monitor.html',
      filename : 'aurora/monitor.html',
      chunks : ['aurora/monitor']
    }),
    new HtmlWebpackPlugin({
      template : './public/toolbox.html',
      filename : 'aurora/toolbox.html',
      chunks : ['aurora/toolbox']
    }),
    new CopyWebpackPlugin([
      {
        from : path.resolve(__dirname, 'public', 'favicon.ico'),
        to : path.resolve(__dirname, 'dist')
      },
      {
        from : path.resolve(__dirname, 'public', 'allSeries.json'),
        to : path.resolve(__dirname, 'dist', 'aurora', 'asset')
      },
      {
        from : path.resolve(__dirname, 'public', 'monitor-allseries.json'),
        to : path.resolve(__dirname, 'dist', 'aurora', 'monitor')
      },
      {
        from : path.resolve(__dirname, 'public', 'def_scene_avatar.png'),
        to : path.resolve(__dirname, 'dist', 'aurora', 'gallery')
      },
      {
        from : path.resolve(__dirname, 'public', 'statusAll.json'),
        to : path.resolve(__dirname, 'dist', 'aurora', 'asset')
      }
    ]),
    new webpack.DefinePlugin({
      __author__ : JSON.stringify('Zdream')
    }),
    // antd 引用了 moment. 这个插件告诉它, 我不需要 moment 的 locale （本地化）文件, 英文就行
    // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
    // 只用中文的话 : new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/)
    new webpack.IgnorePlugin(/\.\/locale$/, /moment$/),
    // new webpack.DllReferencePlugin({
    //   // manifest : path.resolve(__dirname, 'dist', '_lib', 'manifest.json')
    //   name: '_dll_antd',
    //   manifest : path.resolve(__dirname, 'dist', '_lib', 'manifest_ants.json'),
    //   context: path.resolve(__dirname),
    // })
  ],

  resolve : {
    modules : [path.resolve('node_modules')],
    extensions : ['.js', '.css', '.json']
  },

  externals : [
    function(context, request, callback) {
      if (request == 'vue') {
        return callback(null, 'Vue');
      }

      if (/ant-design-vue$/.test(request)){
        console.log(request);
        return callback(null, 'antd');
        // Externalize to a commonjs module using the request path 
        // return callback(null, 'commonjs ' + request);
      }

      // Continue without externalizing the import
      callback();
    }
  ],

  module : {
    rules : [
      {
        test : /\.(png|jpg|gif)$/,
        use : 'file-loader'
      },
      {
        test : /\.js$/,
        exclude : /node_modules/,
        use : [
          {
            loader : 'babel-loader',
            options : {
              presets : [
                '@babel/preset-env'/*, '@babel/preset-react'*/
              ],
              plugins : [
                '@babel/plugin-proposal-class-properties',
                // [
                //   '@babel/plugin-transform-runtime',
                //   {
                //     "absoluteRuntime": false,
                //     "corejs": false,
                //     "helpers": true,
                //     "regenerator": true,
                //     "useESModules": false,
                //     "version": "7.0.0-beta.0"
                //   }
                // ]
              ]
            }
          }
        ]
      },
      {
        test : /\.css$/,
        use : ['style-loader', 'css-loader']
      },
      {
        test : /\.less$/,
        use : 
        [
          'style-loader',
          'css-loader',
          {
            loader : 'less-loader',
            options : {
              javascriptEnabled: true
            }
          }
        ]
      }
    ]
  },

  optimization: {
    splitChunks: {
      chunks: 'all',
    },
  }
}